local pairs = pairs
local ipairs = ipairs
local type = type
module("tier1")

EMPTY = {
	agility = 0,
	intellect = 0,
	spirit = 0,
	stamina = 0,
	strength = 0,
	stats_p = 0,

	maxhealth = 0,
	maxmana = 0,
	armor = 0,

	dodge = 0,
	parry = 0,
	block = 0,

	blockvalue = 0,

	meleehit = 0,
	meleecrit = 0,
	meleehaste = 0,
	meleehit_taken = 0,
	meleecrit_taken = 0,
	meleeattackpower = 0,

	rangedhit = 0,
	rangedcrit = 0,
	rangedhaste = 0,
	rangedhit_taken = 0,
	rangedcrit_taken = 0,
	rangedattackpower = 0,

	spellhit = 0,
	spellcrit = 0,
	spellhaste = 0,
	spellhit_taken = 0,
	spellcrit_taken = 0,

	healing = 0,
	spelldamage = 0,

	manareg = 0,
	healthreg = 0,

	healthreg_fight = 0,
	manareg_fsr_p = 0,
	manareg_fsr = 0,

	skill_defense = 0,

	skill_weapon_dagger = 0,
	skill_weapon_sword = 0,
	skill_weapon_sword_2h = 0,
	skill_weapon_axe = 0,
	skill_weapon_axe_2h = 0,
	skill_weapon_mace = 0,
	skill_weapon_mace_2h = 0,
	skill_weapon_bow = 0,
	skill_weapon_crossbow = 0,
	skill_weapon_gun = 0,
	skill_weapon_fist = 0,
	skill_weapon_staff = 0,
	skill_weapon_polearm = 0,
	skill_weapon_feral = 0,

	expertise = 0,
	resilience = 0,

	ratings = "table",
	modifiers = "table",
	ref_modifiers = "table",
}

function calc(unit)
	local tier1 = {}
	for stat, value in pairs(EMPTY) do
		if value == "table" then
			tier1[stat] = {}
		else
			tier1[stat] = value
		end
	end
	for stat, value in pairs(unit.innate) do
		if type(value) == "number" then
			tier1[stat] = value
		end
	end
	for _, aura in ipairs(unit.auras) do
		local active = true
		if aura.restrict then
			active = false
		end
		if active then
			if aura.type == "stat" then
				local stat = aura.attribute
				tier1[stat] = (tier1[stat] or 0) + aura.value
			elseif aura.type == "rating" then
				local stat = aura.attribute
				tier1.ratings[stat] = (tier1.ratings[stat] or 0) + aura.value
			elseif aura.type == "modifier" then
				local stat = aura.attribute
				if not aura.reference then
					tier1.modifiers[stat] = (tier1.modifiers[stat] or 0) + aura.value
				else
					local t = tier1.ref_modifiers[stat]
					if not t then
						t = {}
						tier1.ref_modifiers[stat] = t
					end
					t[#t + 1] = { reference = aura.reference, multiplier = aura.multiplier }
				end
			end
		end
	end
	return tier1
end
